package router
import (
	"github.com/gin-gonic/gin"
	api "hulk-api/apis"
	"net/http"
)
func InitRouter() *gin.Engine {
	router := gin.Default()
	//IndexApi为一个Handler
	router.Use(Cors())    //设置跨域
	router.Static("/template","./template")
	router.StaticFS("/find_template", http.Dir("template"))
	//workspace
	router.Static("/workspace","./workspace")
	router.StaticFS("/find_workspace", http.Dir("workspace"))
	router.Static("/static","./static")
	router.StaticFS("/find_static", http.Dir("static"))
	//查询文件大小
	router.GET("/api/file/stat", api.GetFileStat)   //注意类似的web目录攻击


	//上传文件
	router.POST("/upload", api.Upload)
	//router.POST("/uploadTestReport", api.UploadReport)
	//router.StaticFile("/favicon.ico", "./assets/a.png")
	//登录接口
	router.POST("/api/login", api.HandleLogin)

	//系统管理接口
	router.GET("/api/sysconfig/users", api.HandleUserManager)
	router.POST("/api/sysconfig/deleteUser", api.DeleteUser)
	router.POST("/api/sysconfig/resetPassword", api.ResetPassword)
	router.POST("/api/sysconfig/addUser", api.AddUser)

	//服务器管理接口
	router.GET("/api/server/assets", api.GetAssets)

	router.GET("/api/server/dbList", api.GetDbList)
	router.GET("/api/server/instanceDbList", api.GetDbListByInstance)    //指定实例的数据库列表
	router.GET("/api/server/tableList", api.GetTables)

	router.GET("/api/server/sqlJobList", api.GetSqlJobList)
	router.POST("/api/server/rejectDbJob", api.RejectDbJob)
	router.POST("/api/server/execDbJob", api.ExecDbJob) //sql job 执行接口
	router.POST("/api/server/addDbJob", api.AddDbJob)



	//router.POST("/api/server/execSelect", api.ExecSelect) //数据库查询接口 SELECT
	router.POST("/api/server/execDbSelect", api.ExecDbSelect) //数据库查询接口 SELECT

	router.GET("/api/server/slowLog", api.GetSlowLog) //数据库查询接口 SELECT

	//ansible
	router.GET("/api/ansible/hosts", api.GetAnsibeHosts)
	router.GET("/api/ansible/ipLists", api.GetIplists)
	router.POST("/api/ansible/editHostGroup", api.UpdateHostGroup)
	router.POST("/api/ansible/addHostGroup", api.AddHostGroup)
	router.GET("/api/ansible/templatePath", api.GetTemplatePath)
	router.POST("/api/ansible/runModule", api.RunAnsibleCMD)
	router.POST("/api/ansible/runPlaybook", api.RunAnsiblePlaybook)

	//CICD
	router.POST("/api/deploy/additem", api.AddDeployItem)
	router.POST("/api/deploy/deleteItem", api.DeleteDeployItem)
	router.GET("/api/deploy/items", api.GetDeployItems)
	router.GET("/api/deploy/item", api.GetDeployItem)
	//创建简单视图
	router.POST("/api/deploy/addview", api.AddDeployView)
	router.POST("/api/deploy/saveview", api.SaveDeployView)
	router.POST("/api/deploy/deleteview", api.DeleteDeployView)

	router.GET("/api/deploy/views", api.GetDeployViews)
	router.GET("/api/deploy/view", api.GetDeployView)
	router.GET("/api/deploy/view/items", api.GetDeployViewItems)

	router.POST("/api/deploy/saveItemConfig", api.SaveConfig)
	router.POST("/api/deploy/startDeploy", api.StartDeploy)
	router.GET("/api/deploy/workspaces", api.GetWorkspace)
	router.GET("/api/deploy/output", api.GetOutput)
	router.POST("/api/deploy/cleanWorkspace", api.CleanWorkspace)
	router.GET("/api/deploy/deployProgress", api.GetDeployTaskProgress)
	router.GET("/api/deploy/queues", api.GetDeployTaskQueue)
	router.GET("/api/deploy/changeConcurrency", api.UpdateDeployConcurrency)
	router.GET("/api/deploy/concurrency", api.GetDeployConcurrency)


	//部署任务-可以当做简单的工作流
	router.POST("/api/deploy/addJob", api.AddDeployJob)
	router.GET("/api/deploy/jobList", api.GetDeployJobs)
	//获取部署任务的工作流进度
	router.GET("/api/deploy/deploymentJobStatus", api.GetDeploymentJobStatus)
	router.GET("/api/deploy/closedeploymentJob", api.CloseDeploymentJob)
	router.POST("/api/deploy/beginDeployJob", api.BeginDeployJob)
	router.POST("/api/deploy/beginDeploySqlJobByHand", api.BeginDeploySqlJobByHand)
	router.GET("/api/deploy/beginDeployItemJob", api.BeginDeployItemJobByHand)
	router.POST("/api/deploy/deleteDeployJob", api.DeleteDeployJob)

	//监控平台
	router.GET("/api/monitor/nodes", api.GetMonitorNodes)
	router.GET("/api/monitor/host", api.GetHostMonitorData)
	router.GET("/api/monitor/mysql", api.GetMysqlMonitorData)
	router.GET("/api/monitor/interface", api.GetInterfaceMonitorData)
	router.POST("/api/monitor/restResponseCodeDistrbution", api.GetRestResponseCodeDistrbution)

	router.POST("/api/monitor/alertSetting", api.AlertSetting)
	router.GET("/api/monitor/alertStatus", api.AlertStatus)

	//容器云K8S平台
	router.GET("/api/kubernetes/node", api.GetK8sNodes)
	router.GET("/api/kubernetes/nodeDetail", api.GetK8sNodeDetail)

	router.GET("/api/kubernetes/namespaces", api.GetK8sNamespaces)
	router.GET("/api/kubernetes/namespaceDetail", api.GetK8sNamespaceDetail)

	router.GET("/api/kubernetes/deployment", api.GetK8sDeploymentDetail)

	router.GET("/api/kubernetes/replicaset", api.GetK8sReplicaSetDetail)
	router.GET("/api/kubernetes/pod", api.GetK8sPodDetail)
	//删除pod
	router.DELETE("/api/kubernetes/pod", api.DeleteK8sPod)
	router.GET("/api/kubernetes/_raw/pod", api.GetK8sPodMetadata)
	router.PUT("/api/kubernetes/_raw/pod", api.UpdateK8sPodMetadata)

	router.GET("/api/kubernetes/configmap", api.GetK8sConfigmap)
	router.GET("/api/kubernetes/secret", api.GetK8sSecret)
	router.GET("/api/kubernetes/job", api.GetK8sJob)
	router.GET("/api/kubernetes/statefulset", api.GetK8sStatefulset)


	router.GET("/api/kubernetes/clusterRoles", api.GetK8sClusterRoles)
	router.GET("/api/kubernetes/persistentvolumes", api.GetK8sPersistentVolumeList)
	router.GET("/api/kubernetes/persistentvolume", api.GetK8sPersistentVolume)
	router.GET("/api/kubernetes/persistentvolumeclaim", api.GetK8sPersistentVolumeClaim)
	router.GET("/api/kubernetes/daemonset", api.GetK8sDaemonsetDetail)

	router.GET("/api/kubernetes/logs", api.GetK8sPodLogDetail)

	router.GET("/api/kubernetes/deployment/templatePath", api.GetDeploymentTemplatePath)

	//通配删除
	router.DELETE("/api/kubernetes/_del/resource", api.DeleteK8sResource)
	router.GET("/api/kubernetes/_raw/resource", api.GetK8sResource)
	router.PUT("/api/kubernetes/_raw/resource", api.UpdateK8sResourceMetadata)


	return router
}

func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {
		method := c.Request.Method
		//fmt.Println(method)
		c.Header("Access-Control-Allow-Origin", "*")
		c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, PATCH, DELETE")
		c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		c.Header("Access-Control-Allow-Credentials", "true")

		// 放行所有OPTIONS方法，因为有的模板是要请求两次的
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		// 处理请求
		c.Next()
	}
}